package com.haidong.service;

import com.haidong.mapper.UserMapper;
import com.haidong.pojo.User;
import com.haidong.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class UserService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     * 登录方法
     * @param username
     * @param password
     * @return
     */
    public User login(String username,String password){
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.select(username,password);
        sqlSession.close();
        return user;
    }

    /**
     *  注册方法
     * @param user
     * @return
     */
    public boolean register(User user){
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = mapper.selectByUsername(user.getUsername());

        if(user1==null){
            mapper.add(user);
            sqlSession.commit();
        }
        sqlSession.close();
        return user1==null;
    }
}
